package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.OrderModel;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/9.
 */

public class OrderDetailAdapter extends RecycleVAdapter<OrderModel> {

    private SimpleDateFormat dateFormat;

    public OrderDetailAdapter(Context context, @NonNull List<OrderModel> list) {
        super(context, list);
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_order_detail;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        OrderModel data = getData(position);

        holder.setTextView(R.id.orderDetailId, "订单号：" + data.getOrderId());
        String orderDescStr = getOrderDestText(data.getOrderState(), data.getPayState());
        TextView orderDescView = holder.getTextView(R.id.orderDetailDesc);
        if(orderDescStr == null || orderDescStr.trim().length() == 0){
            orderDescView.setVisibility(View.GONE);
        } else{
            orderDescView.setVisibility(View.VISIBLE);
            orderDescView.setText(orderDescStr);
        }
        holder.setTextView(R.id.orderDetailTitle, data.getOrderTitle());
        holder.setTextView(R.id.orderDetailTime, dateFormat.format(data.getOrderTime()));
        holder.setTextView(R.id.orderDetailAmount, String.format("已付款：￥%.1f", ((float)data.getOrderAmount())/100));

        if(data.getRelationList() == null || data.getRelationList().size() == 0){
            holder.get(R.id.orderDetailRelationView).setVisibility(View.GONE);
        } else {
            holder.get(R.id.orderDetailRelationView).setVisibility(View.VISIBLE);
            RecyclerView relationView = holder.get(R.id.orderDetailRelationView);
            relationView.setHasFixedSize(true);
            relationView.setLayoutManager(new LinearLayoutManager(mContext));
            relationView.setAdapter(new OrderDetailRelationAdapter(mContext, data.getRelationList()));
        }

        String relationInfo = data.getRelationInfo();
        if(relationInfo == null || relationInfo.trim().length() == 0){
            holder.get(R.id.orderDetailLine).setVisibility(View.GONE);
            holder.get(R.id.orderDetailRelationInfo).setVisibility(View.GONE);
        } else{
            holder.get(R.id.orderDetailLine).setVisibility(View.VISIBLE);
            TextView relationInfoView = holder.getTextView(R.id.orderDetailRelationInfo);
            relationInfoView.setVisibility(View.VISIBLE);
            relationInfoView.setText(relationInfo);
        }
    }

    private String getOrderDestText(int orderState, int payState){
        String payStateStr = null;
        String orderStateStr = null;
        String resultStr = null;
        if(payState == 0)
            payStateStr = "未支付";
        else if(payState == 1)
            payStateStr = "已支付";
        else if(payState == 2)
            payStateStr = "支付失败";
        else if(payState == 3)
            payStateStr = "已退款";
        if(orderState == 1)
            orderStateStr = "待付款";
        else if(orderState == 2)
            orderStateStr = "待完成";
        else if(orderState == 3)
            orderStateStr = "已取消";
        else if(orderState == 4)
            orderStateStr = "待收货";
        else if(orderState == 5)
            orderStateStr = "已完成";
        else if(orderState == 6)
            orderStateStr = "售后中";
        if(payStateStr != null && orderStateStr != null)
            resultStr = payStateStr + "," + orderStateStr;
        else if(payStateStr == null && orderStateStr != null)
            resultStr = orderStateStr;
        else if(payStateStr != null && orderStateStr == null)
            resultStr = payStateStr;
        return resultStr;
    }
}
